﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.Procurement
{
    public class PurchasingsUpdateHandler : IRequestHandler<PurchasingsUpdateCommand, Result>
    {
        /// <summary>
        /// 采购/委外订单头信息
        /// </summary>
        private readonly IBaseRepository<Purchasings> purchasingsRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<PurchasingsUpdateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingsRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        public PurchasingsUpdateHandler(IBaseRepository<Purchasings> purchasingsRepository, IMapper mapper,  ILogger<PurchasingsUpdateHandler> logger)
        {
            this.purchasingsRepository = purchasingsRepository;
            this.mapper = mapper;
            this.logger = logger;
        }
        public async Task<Result> Handle(PurchasingsUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Update };

            try
            {
                var AddData = mapper.Map<Purchasings>(request);
                string sql = "UPDATE Purchasings SET PurchasingNo=@PurchasingNo ,PurchasingName=@PurchasingName ,PurchasingTypeId=@PurchasingTypeId ,ProjectId=@ProjectId,PurchasingDesc=@PurchasingDesc ,SuppliersId=@SuppliersId,OrderAmount=@OrderAmount,Currency =@Currency ,AdvancePayment =@AdvancePayment,StartTime =@StartTime,EndTime=@EndTime,SignUpTime=@SignUpTime ,TaxType=@TaxType ,TaxRate=@TaxRate ,Billing=@Billing,PaidFor=@PaidFor ,DepartmentId=@DepartmentId,ApprovalStatus=@ApprovalStatus,Contract =@Contract,Salesperson =@Salesperson,LatestDate=@LatestDate,tests=@tests,PurchasingStatus =@PurchasingStatus WHERE PurchasinId =@PurchasinId and PurchasingNo=@PurchasingNo";
                var AddNum = purchasingsRepository.Update(sql, new
                {
                    PurchasinId = AddData.PurchasinId,
                    PurchasingNo = AddData.PurchasingNo,
                    PurchasingName = AddData.PurchasingName,
                    PurchasingTypeId = AddData.PurchasingTypeId,
                    ProjectId = AddData.ProjectId,
                    PurchasingDesc = AddData.PurchasingDesc,
                    SuppliersId = AddData.SuppliersId,
                    OrderAmount = request.OrderAmount,
                    Currency = AddData.Currency,
                    AdvancePayment = AddData.AdvancePayment,
                    StartTime = AddData.StartTime,
                    EndTime = AddData.EndTime,
                    SignUpTime = AddData.SignUpTime,
                    PurchasingStatus = AddData.PurchasingStatus,
                    TaxType = AddData.TaxType,
                    TaxRate = AddData.TaxRate,
                    Billing = AddData.Billing,
                    PaidFor = AddData.PaidFor,
                    DepartmentId = AddData.DepartmentId,
                    ApprovalStatus = request.ApprovalStatus,
                    Contract = AddData.Contract,
                    Salesperson = AddData.Salesperson,
                    LatestDate = AddData.LatestDate,
                    tests = AddData.tests,
                });
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "采购/委外订单的修改成功";
                    logger.LogInformation("采购/委外订单的修改成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Update;
                    result.Data = null;
                    result.Msg = "采购/委外订单的修改失败";
                    logger.LogError("采购/委外订单的修改失败，");

                }
            }
            catch (Exception ex)
            {
                logger.LogError($"采购/委外订单的修改失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
